package com.yonyou.lxp.account.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.media.MediaCodec;
import android.net.Uri;
import android.util.Log;

import com.yonyou.lxp.account.entity.ExpenditureInfo;
import com.yonyou.lxp.account.entity.MsgInfo;

import org.xutils.common.util.LogUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 作者： liuxiaopeng on 16/7/5.
 * 描述：
 */

public class MsgUtils {
    private static final String SMS_URI_ALL = "content://sms/";

    public static List<ExpenditureInfo> getMsg(Activity mContext,String time) {

        List<ExpenditureInfo> listData = new ArrayList<>();

        String SP_MSG_LASTTIME = "0";
        if (time==null){
            try {
                SP_MSG_LASTTIME = (String) SPUtils.get(mContext, Constants.SP_MSG_LASTTIME, "0");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            SP_MSG_LASTTIME=time;
        }


        String addrs[] = {"95588", "106980095188", "95599"};
        String where = "";
        for (String addr : addrs) {
            where += " ( address = ? ) or";
        }
        where = where.substring(0, where.length() - 2);
        where = "type = 1 and date > " + SP_MSG_LASTTIME + " and (" + where + " )";

        try {
            Uri uri = Uri.parse(SMS_URI_ALL);
            String[] projection = new String[]{"_id", "address", "body", "date"};
            Cursor cur = mContext.getContentResolver().query(uri, projection, where, addrs, "date desc");      // 获取手机内部短信

            if (cur.moveToFirst()) {
                int index_Address = cur.getColumnIndex("address");
                int index_Body = cur.getColumnIndex("body");
                int index_Date = cur.getColumnIndex("date");
                do {
                    ExpenditureInfo info = new ExpenditureInfo();
                    String strAddress = cur.getString(index_Address);
                    String strbody = cur.getString(index_Body);
                    long longDate = cur.getLong(index_Date);
                    info.setE_time(longDate);
                    info.setE_remarks(strbody);
                    if (Long.parseLong(SP_MSG_LASTTIME) < longDate) {
                        SPUtils.put(mContext, Constants.SP_MSG_LASTTIME, longDate + "");
                        SP_MSG_LASTTIME = longDate + "";
                    }

                    if ("95588".equals(strAddress)) {//工商银行
                        if (strbody.contains("支出")) {
                            String sss[] = null;
                            if (strbody.contains("(消费)")) {
                                sss = strbody.split("\\(消费\\)");
                            } else if (strbody.contains("(ATM取款)")) {
                                sss = strbody.split("\\(ATM取款\\)");
                            } else if (strbody.contains("(b2c)")) {
                                sss = strbody.split("\\(b2c\\)");
                            } else if (strbody.contains("(信使展期)")) {
                                sss = strbody.split("\\(信使展期\\)");
                            }
                            if (sss != null && sss.length > 0) {
                                String ss[] = sss[1].split("元");
                                info.setE_money(ss[0].replace(",", ""));
                                info.setT_type("0");
                                listData.add(info);
                            }
                        } else if (strbody.contains("收入（工资）")) {
                            String sss[] = null;
                            if (strbody.contains("收入（工资）")) {
                                sss = strbody.split("收入（工资）");
                            }
                            if (sss != null && sss.length > 0) {
                                String ss[] = sss[1].split("元");
                                info.setE_money(ss[0].replace(",", ""));
                                info.setT_type("1");
                                listData.add(info);
                            }
                        }
                    } else if ("95599".equals(strAddress)) {//农业银行
                        if (strbody.contains("交易")) {
                            String sss[] = strbody.split("交易");
                            if (sss != null && sss.length > 0) {
                                String ss[] = sss[1].split("元");
                                LogUtil.d(ss[0] + "====" + "农业银行");
                                info.setT_type("0");
                                info.setE_money(ss[0].replace(",", ""));
                                listData.add(info);
                            }
                        }
                    } else if ("106980095188".equals(strAddress)) {//支付宝
                        if (strbody.contains("成功付款")) {
                            String sss[] = strbody.split("成功付款");
                            if (sss != null && sss.length > 0) {
                                String ss[] = sss[1].split("元");
                                LogUtil.d(ss[0] + "====" + "支付宝");
                                info.setT_type("0");
                                info.setE_money(ss[0].replace(",", ""));
                                listData.add(info);
                            }
                        }
                    }
                } while (cur.moveToNext());

                if (!cur.isClosed()) {
                    cur.close();
                    cur = null;
                }
            } else {
                LogUtil.d("无手机短信");
            }
            LogUtil.d("短信查询完成");
            LogUtil.e(SP_MSG_LASTTIME + "===");
        } catch (SQLiteException ex) {
            LogUtil.d("短信查询异常");
        }

        return listData;
    }
}
